//
//  HGCircleVC.m
//  RedWinMall
//
//  Created by HeDianZhao on 2018/7/27.
//  Copyright © 2018年 HeDianZhao. All rights reserved.
//

#import "HGCircleVC.h"
#import "HGCircleCell.h"
#import "HGCircleModel.h"
#import "BaseWKWebviewVC.h"

@interface HGCircleVC ()<UITableViewDelegate, UITableViewDataSource>

@property (strong, nonatomic) UITableView *tableView;

@property (copy, nonatomic) NSMutableArray<HGCircleModel *> *circleModels;

@property (assign, nonatomic) NSInteger page;

@end

@implementation HGCircleVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initSubViews];
    
    [self initDatas];
}


- (void)initDatas {
    _page++;
    [[LMJRequestManager sharedManager] GET:KPHP_Url(@"index.php?act=/Api/Article/indexList") parameters:@{@"page" : [NSNumber numberWithInteger:_page]} completion:^(LMJBaseResponse *response) {
        if (response.responseModel.status == 1) {
            NSArray *models = [HGCircleModel mj_objectArrayWithKeyValuesArray:response.responseModel.data[@"rows"]];
            if (models == nil || models.count == 0) {
                _tableView.mj_footer.state = MJRefreshStateNoMoreData;
                return;
            }
            
            [self.circleModels addObjectsFromArray:models];
            [_tableView reloadData];
        }
        
        [_tableView.mj_header endRefreshing];
        [_tableView.mj_footer endRefreshing];
    }];
}

- (void)initSubViews {
    self.title = @"酒圈子";
    [self.tableView registerClass:[HGCircleCell class] forCellReuseIdentifier:@"HGCircleCell"];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _circleModels.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    HGCircleCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HGCircleCell"];
    cell.model = _circleModels[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    BaseWKWebviewVC *vc = [[BaseWKWebviewVC alloc] initWithUrl:_circleModels[indexPath.row].url];
    vc.diy_title = @"酒圈子";
    [self.navigationController pushViewController:vc animated:YES];
}

- (UITableView *)tableView {
    if (!_tableView){
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:(UITableViewStylePlain)];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.rowHeight = 170;
        _tableView.tableFooterView = [UIView new];
        [self.view addSubview:_tableView];
        
        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            _page = 0;
            [_circleModels removeAllObjects];
            [self initDatas];
        }];
        
        _tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
            [self initDatas];
        }];
        
        [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.left.right.equalTo(self.view);
            make.bottom.offset(-kBottomBarHeight);
        }];
    }
    return _tableView;
}

- (NSMutableArray<HGCircleModel *> *)circleModels {
    if (!_circleModels) {
        _circleModels = [@[] mutableCopy];
    }
    return _circleModels;
}

@end
